package com.appsspot.server;

import java.util.ArrayList;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.appsspot.client.Constants;
import com.appsspot.client.parcer.ConfigClient;
import com.appsspot.client.parcer.Element;
import com.appsspot.client.parcer.SubTab;
import com.appsspot.client.parcer.SubTabElements;
import com.appsspot.client.parcer.Tab;
import com.appsspot.server.parser.SAXPars;

public class ServletContextListenerImpl implements ServletContextListener {

	@Override
	public void contextInitialized(ServletContextEvent servletContextEvent) {
		try {
			SAXPars.parsing("Data.xml");
			
			printDebugInfo();
			
			ServletContext context = servletContextEvent.getServletContext();
			context.setAttribute(
					Constants.k_ATTR_SERVLET_MAIN_MENU,
					ConfigClient.getInstants().getArrayTab());
			context.setAttribute(
					Constants.k_ATTR_SERVLET_TOP_BAR,
					ConfigClient.getInstants().getTopToolStripLinks());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void contextDestroyed(ServletContextEvent servletContextEvent) {
		ServletContext context = servletContextEvent.getServletContext();
		context.removeAttribute(Constants.k_ATTR_SERVLET_MAIN_MENU);
		context.removeAttribute(Constants.k_ATTR_SERVLET_TOP_BAR);
	}
	
	private void printDebugInfo()
	{
		if (ConfigClient.debugInfo)
		{
			System.out.println("=======================================");
			System.out.println("============Load_tab_data==============");
			System.out.println("=======================================");
			ArrayList<Tab> arrTab = ConfigClient.getInstants().getArrayTab();
			for (Tab tab : arrTab) {
				System.out.println("<---------Tab---------->");
				System.out.println(tab.getName());
				ArrayList<SubTab> subTabs = tab.getSubTabs();
				for (SubTab subTab : subTabs) {
					System.out.println("	<-------SubTab------>");
					System.out.println("    "+subTab.getSubTabName());
					System.out.println("    "+subTab.getSubTabUrl());
				}
			}
			
			System.out.println("=======================================");
			System.out.println("========Load SubTabElements data=======");
			System.out.println("=======================================");
			ArrayList<SubTabElements> arrSubTabElements = ConfigClient.getInstants().getArraySubTabElements();
			for (SubTabElements subTabElements : arrSubTabElements) {
				System.out.println("<-------SubTabElements----->");
				System.out.println(subTabElements.getSubTab().getSubTabName());				
				System.out.println(subTabElements.getSubTab().getSubTabUrl());
				ArrayList<Element> elements= subTabElements.getArrElements();
				for (Element element : elements) {
					System.out.println("	<-------element----->");
					System.out.println("    "+element.getAbout());
					System.out.println("    "+element.getCodId());
					System.out.println("    "+element.getImageUrl());
					System.out.println("    "+element.getNameUrl());
				}
			}
		}
	}
}
